package engine.newUi
{
	import engine.control.KitaWebServices;
	import engine.data.ModelLocator;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.StatusEvent;
	import flash.net.LocalConnection;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	import flashx.textLayout.container.ScrollPolicy;
	
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;
	import newx.utils.coders.MD5;

	public class ChangeLogic
	{
		public static var _self:ChangeLogic; 
		public static function getInstance():ChangeLogic
		{
			if(_self==null)
			{
				_self= new ChangeLogic(); 
			}
			return _self;
		}
		public var _ui:ChangeUI  ;
		private var _model:ModelLocator;
		private var _loder:URLLoader ; 
		private var _mc:MovieClip ; 
		private var _conn:LocalConnection;
		private var _arrID:Array = new Array(); 
		private var _url:URLRequest ;
		private var _returnArr:Array= new Array(); 
		public function ChangeLogic()
		{
			
			
		}
		public function Init():void
		{
			_ui = ChangeUI.getInstance();  
			_model = ModelLocator.getInstance();  
			_ui.input_text.multiline = false ; 
			
			_ui.input_text.wordWrap =false ; 
			_ui.input_text.verticalScrollPolicy=ScrollPolicy.OFF;
			_loder= new URLLoader(); 
//			_conn= new LocalConnection();
//			_conn.client=  this ;
//			try{
//				_conn.connect("JXJHConnection"); 
//			}catch(error:ArgumentError){
//				trace("连接错误");
//			}		
			_model.addEventListener(String(CustomToClientFlag.SERVER_CUSTOMMSG_ACCOUNT_INFO),getTipsInfo);
		}
		public function BeforeShow():void
		{
//			_url = new URLRequest("http://61.155.156.159:90/services/ImprestQueryWebService?wsdl");
//			var loader:Loader = new Loader();
//			loader.load(_url);
//			loader.addEventListener(Event.COMPLETE,function (e:Event):void
//			{
//				e.target; 
//			});
			_ui.label_1.htmlText =StaticIni.getSwfLangStr("UIString_1000716") ;
			_ui.input_text.text="" ; 
			addListener();

			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUEST_ACCOUNT_ID);
			
		}

		public function AfterHide():void
		{
			removeListener();
			_mc = null ; 
		}
		
		private function addListener():void
		{
			_ui.sure_btn.addEventListener(MouseEvent.CLICK,SureHandler);
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE , closeHandler);
			_ui.close_btn.addEventListener(MouseEvent.CLICK,cancelHandler);
//			_conn.addEventListener(StatusEvent.STATUS,onStatus); 
		}
		private function onStatus(event:StatusEvent):void
		{
			switch (event.level) {
				case "status":
//					trace("LocalConnection.send() succeeded");
					break;
				case "error":
//					trace("LocalConnection.send() failed");
					break;
			}

		}
		private function removeListener():void
		{
			_ui.sure_btn.removeEventListener(MouseEvent.CLICK , SureHandler);
		}
		private function SureHandler(e:MouseEvent):void
		{
			if(_ui.input_text.text!="")
			{
				var arrid:Array = _arrID.concat();
				arrid.push(_ui.input_text.text);
				changeInfo(arrid);
//				_conn.send("myConnection", "lcHandler",arrid);
			}else
			{
				NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("UIString_1000625"));
			}
			
		}
		
		public function jpHandler():void
		{
//			var str:String = _returnArr[2]
//			var value:int   = int(str.valueOf());
//			trace(str)
			if(_returnArr[2]=="1")
			{
//				NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("UIString_1000626"));
				SystemSender.getInstance().CustomMsgEx(CustomToServerFlag.CLIENT_CUSTOMMSG_FRESHMANCARD_KEY,_returnArr);
				_ui.hide();
			}else
			{
				NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("UIString_1000627"));
				_ui.input_text.text="";
			}
			
//			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_FRESHMANCARD_KEY,arr);
		}
		private function closeHandler(e:NxWindowEvent):void
		{
			_ui.showOrHide();
		}
		private function cancelHandler(e:MouseEvent):void
		{
			_ui.showOrHide();
		}
		//
		private function getTipsInfo(e:CustomMsgEvent):void
		{
			_arrID.splice(0);
			_arrID=e.pList;
		}
		/**
		 * 
		 */		
		private function changeInfo(arr:Array):void
		{
			var p_data:Array = new Array();
			//串
			var array:Array =_model.mainIni.getContent("chage","accessid").split(",");
			//地址
			var str:String  = _model.mainIni.getContent("chage","url");
			//函数接口
			var str_1:String  = _model.mainIni.getContent("chage","accesstype");
			
			var kws:KitaWebServices = new KitaWebServices(str);
			//账号
			var message:String = arr[3] ;
//			var fixStr:String=MD5.hash(("572zSyji2Xfol3f6"+message+""+arr[0]+arr[1]+arr[2]
//									+"KgKROpRQEq35whL")).toLocaleUpperCase();
			
			var fixStr:String=MD5.hash(((array[0]+array[1]+array[2]).toString()+message+""+arr[0]+arr[1]+arr[2]
				+array[3].toString())).toLocaleUpperCase();
			
			
//			kws.call("imprestPropsByGameServer",
//				testReturn,"57","2zSyji2Xfol3f",
//				"6",
//				message,
//				"",
//				arr[0],
//				arr[1],
//				arr[2],
//				fixStr);
			kws.call(str_1,
				testReturn,array[0].toString(),array[1].toString(),
				array[2].toString(),
				message,
				"",
				arr[0],
				arr[1],
				arr[2],
				fixStr);
//			572zSyji2Xfol3f6
//			var ato : AsyncToken =web.imprestPropsByGameServer(
//				"16",			 // * @param accessId  57
//				"2O3I4J21L2K3J", //* @param accessPasswd 2zSyji2Xfol3f
//				"1",				// * @param accessType 6
//				val_0[3],卡号
//				"",
//				val_0[0],账号ID
//				val_0[1], 服务器ID
//				val_0[2], 客户端IP
//				MD5.hash(
//					"162O3I4J21L2K3J1"+val_0[3]+""+val_0[0]+val_0[1]+val_0[2]
//					+"A223L1J2H3S0DF98SKLJ").toLocaleUpperCase()
//			);
		}
		private function testReturn(xml:XML):void
		{
			var value:XMLList=xml.elements().elements().elements()
			var strrrrr:String=value.toString();
			_returnArr=strrrrr.match(/"\S*"/g);
			_returnArr	=	_returnArr.map(function(str:String,index:int,ar:Array):String{return str.substr(1,str.length-2)});
			jpHandler();
		}
		public function getItem(srcviewid:int , srcpos :int , destviewid :int ,destpos :int , amount :int = 0 ):void
		{
			
		}
		
		
	}
}